import { Component, OnInit, OnDestroy } from '@angular/core';
import {EventService, EventMessage, EventSource} from '../services/event.service';
import { RequestReplyService } from '../services/request-reply.service';
import { Settings } from '../entity/TT';
import { Reply } from '../entity/Reply';

@Component({
  selector: 'app-settings',
  templateUrl: './settings.component.html',
  styleUrls: ['./settings.component.scss']
})
export class SettingsComponent implements OnInit, OnDestroy {

  set: Settings;

  constructor(
    private eventService: EventService,
    private requestService: RequestReplyService) 
  { 
    

    this.eventService.subscribeEvent((msg: EventMessage) => {
      switch (msg.source) {
        case EventSource.SET_VOICE_VALUE:
          this.set.voiceValue = msg.data;
          this.save()
          break;

          case EventSource.SET_THEME_VALUE:
              this.set.themeColor = msg.data;
              this.save()
          break;
      }



    });

  }

  ngOnInit() {
    
    this.requestService.GetSetting().subscribe((set:Settings)=>{
      this.set = set;
      this.eventService.sendEvent(EventSource.SET_CHANGED, set);
    
    })
  }

  ngOnDestroy(){
    console.log("SettingsComponent.ngOnDestroy...")
  }

  save(){
    this.requestService.SaveSetting(this.set).subscribe(r =>{
      console.log("=>",r)
    });
  }

  exit(){
    this.eventService.sendExit()
  }
}
